package com.bwie.controller;

import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.*;
import com.bwie.common.domain.request.LottoListReq;
import com.bwie.common.domain.request.LottoLogListReq;
import com.bwie.common.domain.request.LottoRuleAddReq;
import com.bwie.common.domain.request.RuleLogListReq;
import com.bwie.common.domain.response.DrawRes;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.common.utils.OssUtil;
import com.bwie.service.LottoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @className: LottoController
 * @Description: TODO
 * @version: v1.８.0
 * @author: 汪菲阳
 * @date: 2025/8/26 17:45
 */
@CrossOrigin
@RestController
@RequestMapping("lotto")
public class LottoController {
    @Autowired
    private LottoService lottoService;
    @Autowired
    private OssUtil ossUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 抽奖列表
     * @param lottoListReq
     * @return
     */
    @PostMapping("lottoList")
    public Result<PageResult<Lotto>> lottoList(@RequestBody LottoListReq lottoListReq){
        PageHelper.startPage(lottoListReq.getPageNum(),lottoListReq.getPageSize());
        List<Lotto> lottoList=lottoService.lottoList(lottoListReq);
        PageInfo<Lotto> lottoPageInfo = new PageInfo<>(lottoList);
        return PageResult.toResult(lottoPageInfo.getTotal(),lottoPageInfo.getList());
    }

    /**
     * 抽奖记录列表
     * @param lottoLogListReq
     * @return
     */
    @PostMapping("lottoLogList")
    public Result<PageResult<LottoLog>> lottoLogList(@RequestBody LottoLogListReq lottoLogListReq){
        PageHelper.startPage(lottoLogListReq.getPageNum(),lottoLogListReq.getPageSize());
        List<LottoLog> lottoLogList=lottoService.lottoLogList(lottoLogListReq);
        PageInfo<LottoLog> lottoLogPageInfo = new PageInfo<>(lottoLogList);
        return PageResult.toResult(lottoLogPageInfo.getTotal(),lottoLogPageInfo.getList());
    }

    /**
     * 字典下拉框
     * @param dicType
     * @return
     */
    @GetMapping("getDic/{dicType}")
    public Result<List<Dic>> getDic(@PathVariable("dicType") String dicType){
        return Result.success(lottoService.getDic(dicType));
    }

    /**
     * 新增积分抽奖
     * @param lottoRuleAddReq
     * @return
     */
    @PostMapping("addLotto")
    public Result addLotto(@RequestBody LottoRuleAddReq lottoRuleAddReq){
        lottoService.addLotto(lottoRuleAddReq);
        return Result.success();
    }

    /**
     * 上传图片
     * @param file
     */
    @PostMapping("uploadImage")
    public Result uploadImage(MultipartFile file){
        String imgUrl = ossUtil.uploadMultipartFile(file);
        return Result.success(imgUrl);
    }

    /**
     * 规则列表（添加）
     * @return
     */
    @GetMapping("ruleList")
    public Result<List<Rule>> ruleList(){
        return Result.success(lottoService.ruleList());
    }

    /**
     * 抽奖
     * @param lottoId
     * @return
     */
    @GetMapping("sweepstake/{lottoId}")
    public Result sweepstake(@PathVariable("lottoId") Integer lottoId){
        DrawRes drawRes=lottoService.sweepstake(lottoId);
        return Result.success(drawRes);
    }

    /**
     * 用户签到
     * @return
     */
    @GetMapping("singIn")
    public Result<PointsLog> singIn(){
        User userInfo = getUserInfo();
        Integer userId = userInfo.getUserId();
        lottoService.singIn(userId);
        return Result.success();
    }

    /**
     * 获取登录人信息
     * @return
     */
    public User getUserInfo(){
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        return (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
    }

    /**
     * 用户查看规则列表
     * @param lottoId
     * @return
     */
    @GetMapping("ruleLogList/{lottoId}")
    public Result<List<RuleLogListReq>> ruleLogList(@PathVariable("lottoId") Integer lottoId){
        List<RuleLogListReq> logList=lottoService.ruleLogList(lottoId);
        return Result.success(logList);
    }

    /**
     * 判断用户的积分是否充足
     * @param consumePoints
     * @return
     */
    @GetMapping("getUserIntegral/{consumePoints}")
    public Result getUserIntegral(@PathVariable("consumePoints") Integer consumePoints){
        lottoService.getUserIntegral(consumePoints);
        return Result.success();
    }
}
